package com.augurit.common.sysfile.convert;

import java.util.ArrayList;
import java.util.List;

import org.springside.modules.orm.Page;

import com.augurit.common.entity.SysFile;
import com.augurit.common.entity.SysFileLink;
import com.augurit.common.sysfile.web.form.SysFileForm;

public class SysFileConverter {

	public static void convert(SysFileForm form, SysFile entity){
		if(form != null && entity != null){
			entity.setSysFileId(form.getSysFileId());
			entity.setFileCode(form.getFileCode());
			entity.setFileName(form.getFileName());
			entity.setFileSize(form.getFileSize());
			entity.setFileType(form.getFileType());
			entity.setFilePath(form.getFilePath());
			entity.setFileFormat(form.getFileFormat());
			entity.setCmp(form.getCmp());
			entity.setCdt(form.getCdt());
			entity.setEemp(form.getEemp());
			entity.setEdt(form.getEdt());
			entity.setMemo1(form.getMemo1());
			entity.setMemo2(form.getMemo2());
		}
	}
	
	public static SysFileForm convertToForm(SysFile entity){
		if(entity != null){
			SysFileForm form = new SysFileForm();
			form.setSysFileId(entity.getSysFileId());
			form.setFileCode(entity.getFileCode());
			form.setFileName(entity.getFileName());
			form.setFileSize(entity.getFileSize());
			form.setFileType(entity.getFileType());
			form.setFilePath(entity.getFilePath());
			form.setFileFormat(entity.getFileFormat());
			form.setCmp(entity.getCmp());
			form.setCdt(entity.getCdt());
			form.setEemp(entity.getEemp());
			form.setEdt(entity.getEdt());
			form.setMemo1(entity.getMemo1());
			form.setMemo2(entity.getMemo2());
			return form;
		}else
			return null;
	}
	
	public static SysFileForm convertToForm(SysFileLink entity){
		if(entity != null){
			SysFileForm form = new SysFileForm();
			form.setSysFileId(entity.getSysFileId());
			form.setEntity(entity.getEntity());
			form.setEntityId(entity.getEntityId());
			return form;
		}
		return null;
	}
	
	public static List<SysFileForm> convertToFormList(List<SysFile> list){
		if(list != null && list.size() > 0){
			List<SysFileForm> result = new ArrayList();
			for(SysFile entity : list){
				SysFileForm form = convertToForm(entity);
				result.add(form);
			}
			return result;
		}else
			return null;
	}
	
	public static List<SysFileForm> convertToLinkFormList(List<SysFileLink> list){
		if(list != null && list.size() > 0){
			List<SysFileForm> result = new ArrayList();
			for(SysFileLink entity: list){
				SysFileForm form = convertToForm(entity);
				result.add(form);
			}
			return result;
		}
		return null;
	}

	/**
	 * 将查询结果的VO对象列表转换为Form对象列表
	 * @param page 含有VO对象列表查询结果的Page对象
	 * @return
	 */
	public static List<SysFileForm> pageResultConvertToFormList(Page page){
		List<SysFileForm> list = null;
		
		//将查询结果的VO对象列表转换为Form对象列表
		if(page != null && page.getResult() != null && page.getResult().size() > 0){
			//构建返回对象列表
			list = new ArrayList();
			
			//转换
			List<Object[]> result = page.getResult();
			for(Object[] objs : result){
				SysFile entity = (SysFile)objs[0];
				SysFileForm form = convertToForm(entity);
				form.setEntity((String)objs[1]);
				form.setEntityId((Long)objs[2]);
				list.add(form);
			}
		}
		
		return list;
	}
	
	/**
	 * 转换排序HQL分句
	 * @param orderProperty 排序字段
	 * @param orderDirection 排序顺序。ASC或DESC
	 * @return
	 */
	public static String constructOrderBy(String orderProperty, String orderDirection){
		String result = "";
		
		if(orderProperty != null && orderProperty.trim().length() > 0
				&& orderDirection != null && orderDirection.trim().length() > 0){
			if(orderProperty.equals("entity") || orderProperty.equals("entityId")){
				result += " order by link." + orderProperty + " " + orderDirection;
			}else{
				result += " order by file." + orderProperty + " " + orderDirection;
			}
		}
		
		return result;
	}
}
